﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common;

namespace AccessToDataLibrary
{
    public partial class AccessToDataLibrary
    {
        public List<Magazine> GetAllMagazines()
        {
            DataRow[] searchedRow = libraryDataSet.Magazines.Select();
            return GetRestPartsOfMagazines(searchedRow);
        }

        private List<Magazine> GetRestPartsOfMagazines(DataRow[] searchedRow)
        {
            if (searchedRow == null || searchedRow.Length == 0)
                return null;

            List<Magazine> magazines = new List<Magazine>();
            foreach (UniversityLibraryDataSet.MagazinesRow magazineRow in searchedRow)
            {
                Magazine magazine = new Magazine();
                magazine.IssueNumber = magazineRow.IssueNumber;
                if (magazineRow.ItemsRow != null)
                {
                    magazine.ID = magazineRow.ItemsRow.ID;
                    magazine.Name = magazineRow.ItemsRow.Name;
                    magazine.Publisher = magazineRow.ItemsRow.Publisher;
                    magazine.PublishedDate = magazineRow.ItemsRow.PublishedDate;
                }
                else
                {
                    throw new Exception("Cannot find corresponding Item");
                }

                magazines.Add(magazine);
            }

            return magazines;
        }

        public List<Magazine> GetSearchedMagazines(Magazine magazine)
        {
            UniversityLibraryDataSet.ItemsRow[] searchedItemsRows =
                (UniversityLibraryDataSet.ItemsRow[])libraryDataSet.Items.Select(MakeFilteredQuery(magazine.ItemFields));

            if (searchedItemsRows == null || searchedItemsRows.Length == 0)
            {
                return new List<Magazine>(0);
            }

            List<UniversityLibraryDataSet.ItemsRow> searchedItemsRowsList =
                searchedItemsRows.ToList<UniversityLibraryDataSet.ItemsRow>();

            List<UniversityLibraryDataSet.MagazinesRow> searchedMagazinesRowsList =
                new List<UniversityLibraryDataSet.MagazinesRow>();

            foreach(UniversityLibraryDataSet.ItemsRow itemRow in searchedItemsRowsList)
            {
                UniversityLibraryDataSet.MagazinesRow[] magRows = itemRow.GetMagazinesRows();
                if (magRows != null && magRows.Length > 0)
                {
                    searchedMagazinesRowsList.Add(magRows[0]);
                }

            }
            List<UniversityLibraryDataSet.MagazinesRow> realMagazinesRows =
                searchedMagazinesRowsList;

            if (magazine.IssueNumber != string.Empty)
            {
                realMagazinesRows = searchedMagazinesRowsList.FindAll
                    (val => val.IssueNumber == magazine.IssueNumber);
            }

            if (realMagazinesRows == null || realMagazinesRows.Count == 0)
                return new List<Magazine>(0);

            return GetRestPartsOfMagazines(realMagazinesRows.ToArray());
        }

        public List<Magazine> AddMagazine(List<Magazine> magazines)
        {
            List<Magazine> notUpdateMag = new List<Magazine>();
            foreach (Magazine magazine in magazines)
            {
                bool canAdd = IsUniqueMagazineInDB(magazine);

                if (!canAdd)
                {
                    notUpdateMag.Add(magazine);
                    continue;
                    
                }
                UniversityLibraryDataSet.ItemsRow itemsRow =
                    libraryDataSet.Items.AddItemsRow(Guid.NewGuid(), magazine.Name, magazine.Publisher,
                    magazine.PublishedDate);
                libraryDataSet.Magazines.AddMagazinesRow(itemsRow, magazine.IssueNumber);
            }
            provider.UpdateAllData();
            return notUpdateMag;

        }

        private bool IsUniqueMagazineInDB(Magazine magazine)
        {
            DataRow[] itemsRows = libraryDataSet.Items.Select("Name = '" + magazine.Name + "'");
            if (itemsRows != null && itemsRows.Length > 0)
            {
                foreach (DataRow itemRow in itemsRows)           
                {
                    UniversityLibraryDataSet.MagazinesRow[] magazinesRows =
                        ((UniversityLibraryDataSet.ItemsRow)itemRow).GetMagazinesRows();

                    foreach (UniversityLibraryDataSet.MagazinesRow magazineRow in magazinesRows)
                    {
                        if (magazine.IssueNumber == magazineRow.IssueNumber)
                        {
                            return false;
                        }
                    }
                }
                    
            }
            return true;
        }

        public List<string> GetAllMagazinesNames()
        {
            List<string> magazinesNames = new List<string>();
            DataRowCollection magazinesRows = libraryDataSet.Magazines.Rows;
            foreach (DataRow magazinRow in magazinesRows)
            {
                magazinesNames.Add(((UniversityLibraryDataSet.MagazinesRow)magazinRow).ItemsRow.Name);
            }


            return magazinesNames.Distinct().ToList();
        }

        public List<string> GetCurrentMagazineAllIssueNumber(string magazineName)
        {
            List<string> magazineIssueNumbers = new List<string>();

            UniversityLibraryDataSet.ItemsRow[] itemsRows =
                (UniversityLibraryDataSet.ItemsRow[])libraryDataSet.Items.Select("Name ='" + magazineName + "'");

            foreach (UniversityLibraryDataSet.ItemsRow itemRow in itemsRows)
            {
                magazineIssueNumbers.Add(itemRow.GetMagazinesRows()[0].IssueNumber);
            }

            return magazineIssueNumbers;
        }

    }
}
